#include"queue.h"
void Inti(QPlist* plist)
{
	assert(plist);
	plist->tail = NULL;
	plist->head = NULL;
	plist->size = 0;
}

void Destroy(QPlist* plist)
{
	assert(plist);
	while (plist->head)
	{
		if (plist->head == plist->tail)
		{
			free(plist->head);
			free(plist->tail);
			plist->head = NULL;
		}
		else
		{
			QNode* next = plist->head->next;
			free(plist->head);
			plist->head = next;
		}
	}
	plist = NULL;
}

bool isEmpty(QPlist* plist)
{
	assert(plist);
	return plist->size == 0;
}

void Pushtail(QPlist* plist, queuetype x)
{
	assert(plist);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc");
		return;
	}
	newnode->data = x;
	newnode->next = NULL;
	if (plist->head == NULL)
	{
		plist->head = plist->tail = newnode;
	}
	else
	{
		plist->tail->next = newnode;
		plist->tail = plist->tail->next;
	}
	plist->size++;
}

void Delete(QPlist* plist)
{
	assert(plist);
	QNode* next = plist->head->next;
	free(plist->head);
	plist->head = next;
	plist->size--;
	if (plist->head == NULL)
	{
		plist->tail = NULL;
	}
}

int plistsize(QPlist* plist)
{
	assert(plist);
	return plist->size;
}

queuetype plistfront(QPlist* plist)
{
	assert(plist);
	assert(plist->head);
	return plist->head->data;
}

queuetype plistback(QPlist* plist)
{
	assert(plist);
	assert(plist->tail);
	return plist->tail->data;
}